/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.process;

import java.util.HashSet;

import org.openaecfm.model.datetime.WorkTime;
import org.openaecfm.model.kernel.Control;
import org.openaecfm.model.process.WorkCalendar;
import org.openaecfm.model.process.WorkCalendarTypeEnum;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class WorkCalendarIO extends
		AbstractEntityInstanceInputImpl<WorkCalendar> implements
		IEntityInstanceOutput<WorkCalendar> {

	private IEntityInstanceUtilOutput<Control> utilOutput;
	private IEntityInstanceUtilInput<Control> utilInput;

	public void setUtilOutput(IEntityInstanceUtilOutput<Control> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(IEntityInstanceUtilInput<Control> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public String exportSPF(WorkCalendar pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCWORKCALENDAR",
				appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set WorkingTimes
		if (pojo.getWorkingTimes() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (WorkTime w : pojo.getWorkingTimes()) {
				l.add(new EntityInstanceNameParameter(appDataManager
						.getName(w)));
			}
			ei.addParameter(l);
		}

		// Set ExceptionTimes
		if (pojo.getExceptionTimes() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (WorkTime w : pojo.getExceptionTimes()) {
				l.add(new EntityInstanceNameParameter(appDataManager
						.getName(w)));
			}
			ei.addParameter(l);
		}

		// Set PredefinedType
		if (pojo.getPredefinedType() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new EnumerationParameter(pojo.getPredefinedType()));
		}

		return ei.toString();
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei, WorkCalendar pojo,
			IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set WorkingTimes
		Parameter workingTimes = sei.getParameter(6);
		if (workingTimes.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) workingTimes;
			if (pojo.getWorkingTimes() == null) {
				pojo.setWorkingTimes(new HashSet<WorkTime>());
			}
			for (Parameter p : l) {
				EntityInstanceNameParameter e = (EntityInstanceNameParameter) p;
				pojo.getWorkingTimes()
						.add((WorkTime) appDataManager.getObject(e
								.toInstanceName()));
			}
		}

		// Set ExceptionTimes
		Parameter exceptionTimes = sei.getParameter(7);
		if (exceptionTimes.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) exceptionTimes;
			if (pojo.getExceptionTimes() == null) {
				pojo.setExceptionTimes(new HashSet<WorkTime>());
			}
			for (Parameter p : l) {
				EntityInstanceNameParameter e = (EntityInstanceNameParameter) p;
				pojo.getExceptionTimes()
						.add((WorkTime) appDataManager.getObject(e
								.toInstanceName()));
			}
		}
	}

	@Override
	protected WorkCalendar doCreateObject() {
		return new WorkCalendar();
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			WorkCalendar pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set PredefinedType
		Parameter predefinedType = sei.getParameter(8);
		if (predefinedType.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) predefinedType;
			pojo.setPredefinedType(WorkCalendarTypeEnum.valueOf(e
					.toEnumString()));
		}
	}
}
